﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HHBLL;
using HHCommon;
using HHModel;
using HHDBUtility;
using System.Data;


namespace HHWeb.SystemManage.DownLoadCodes
{
    public partial class Dispose : Base
    {
        HHBLL.DownloadCodes B = new HHBLL.DownloadCodes();
        HHModel.DownloadCodes M = new HHModel.DownloadCodes();
        HHCommon.Help help = new Help();
        public string DowncodeNumber = string.Empty;
        public string DowncodeName = string.Empty;
        public string Downcode = string.Empty;
        public int IfUse = 0;
        public string Expiration = string.Empty;
        public int Status = 1;
        public string AddTime = string.Empty;
        public string Command = "Add";
        public string Message = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (help.GetHttpMethodBool())
                {
                    Command = Request.Form["Command"].ToString();
                    switch (Command)
                    {
                        case "Add":
                            Message = Add();
                            break;
                        case "Update":
                            Message = Update();
                            break;
                        default:
                            break;
                    }
                    if (Message.EndsWith("成功！"))
                        MessageBox.ShowAndRedirect(this.Page, Message, "List.aspx?DowncodeNumber=" + DowncodeNumber + "");
                    else
                        MessageBox.ResponseScript(this.Page, Message, "javascript:history.go(-1)");
                }
                else
                {
                    try
                    {
                        int ID = Convert.ToInt32(Request.QueryString["ID"].ToString());
                        M = B.GetModel(ID);
                        if (M != null)
                        {
                            DowncodeNumber = M.DowncodeNumber;
                            Downcode = M.Downcode;
                            Status = Convert.ToInt32(M.Status);
                            IfUse = Convert.ToInt32(M.IfUse);
                            Expiration = Convert.ToDateTime(M.Expiration).ToString("yyyy-MM-dd HH:mm:ss");
                            AddTime = Convert.ToDateTime(M.AddTime).ToString("yyyy-MM-dd HH:mm:ss");
                            Command = "Update";
                        }
                        else
                        {
                            MessageBox.ShowAndRedirect(this.Page, "该数据不存在！", "List.aspx");
                            return;
                        }
                    }
                    catch
                    {
                        DowncodeNumber = Request.QueryString["DowncodeNumber"].ToString();
                        Downcode = help.GetRandom();
                        Expiration = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        AddTime = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    }
                }
            }
            catch
            {
                MessageBox.ResponseScript(this.Page, "操作错误，请联系管理员！", "javascript:history.go(-1)");
                return;
            }
        }

        #region //添加下载码
        public string Add()
        {
            M.Downcode = Request.Form["Downcode"].ToString();
            M.DowncodeNumber = Request.Form["DowncodeNumber"].ToString();
            DowncodeNumber = Request.Form["DowncodeNumber"].ToString();
            M.Expiration = Convert.ToDateTime(Request.Form["Expiration"]);
            M.IfUse = int.Parse(Request.Form["IfUse"]);
            M.Status = int.Parse(Request.Form["Status"]);
            M.AddTime = Convert.ToDateTime(Request.Form["AddTime"]);
            if (B.Add(M) > 0)
                return "添加成功！";
            else
                return "添加失败！";
        }
        #endregion

        #region //修改下载码
        public string Update()
        {
            M.ID=Convert.ToInt32(Request.QueryString["ID"].ToString());
            M.Downcode = Request.Form["Downcode"];
            M.DowncodeNumber = Request.Form["DowncodeNumber"];
            DowncodeNumber = Request.Form["DowncodeNumber"].ToString();
            M.Expiration = Convert.ToDateTime(Request.Form["Expiration"]);
            M.IfUse = int.Parse(Request.Form["IfUse"]);
            M.Status = int.Parse(Request.Form["Status"]);
            M.AddTime = Convert.ToDateTime(Request.Form["AddTime"]);
            if (B.Update(M))
                return "修改成功！";
            else
                return "修改失败！";
        }
        #endregion
    }
}